----------------------------------------------
-- infDK
----------------------------------------------

local _, enClass = UnitClass("player")
if enClass ~= "DEATHKNIGHT" then return end

infDK = LibStub("AceAddon-3.0"):NewAddon("infDK", "AceEvent-3.0", "AceBucket-3.0")

local pairs = pairs
local IsShown = IsShown
local UnitBuff = UnitBuff
local GetTexture = GetTexture
local SetCooldown = SetCooldown
local ActionButton_ShowOverlayGlow = ActionButton_ShowOverlayGlow
local ActionButton_HideOverlayGlow = ActionButton_HideOverlayGlow

local player_buffs = {
	["Blood Charge"] = false, --114851
	["Dark Simulacrum"] = false, --77606
	["Conversion"] = false --119975
}

local pet_buffs = {
	["Shadow Infusion"] = false, --91342
	["Dark Transformation"] = false, --63560
}

function infDK:OnInitialize()
	self:FrameCreation()

	self:RegisterBucketEvent("UNIT_AURA", 0.1, "UNIT_AURA")
	self:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
	self:RegisterEvent("UNIT_PET")
end

function infDK:FrameCreation()
	local size = 40
	local backdrop = {
		bgFile = nil, 
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		edgeSize = 12,
		tile = false,
		insets = {left = 1, right = 1, top = 1, bottom = 1},
	}

	--Blood Tap
	tap_icon = CreateFrame("Frame", nil, UIParent)
	tap_icon:SetSize(size, size)
	tap_icon:SetPoint("CENTER", UIParent, "CENTER", 0, -82)
	tap_icon.cdtexture = CreateFrame("Cooldown", nil, tap_icon)
	tap_icon.cdtexture:SetReverse(true)
	tap_icon.cdtexture:SetAllPoints(true)
	tap_icon.cdtexture.noCooldownCount = true
	tap_icon.cdtexture:SetFrameLevel(1)
	tap_icon:Hide()

	tap_texture = tap_icon:CreateTexture(nil, "BACKGROUND")
	tap_texture:SetAllPoints(true)
	tap_texture:SetTexture("Interface\\Icons\\Spell_Deathknight_Bloodtap")
	--tap_texture:SetDesaturated(1) --greyscale
	--tap_texture:SetTexCoord(.07, .9, .07, .90) --zoomed

	tap_border = CreateFrame("Frame", nil, tap_icon)
	tap_border:SetAllPoints(true)
    tap_border:SetBackdrop(backdrop)

	tap_text = tap_icon.cdtexture:CreateFontString(nil, "OVERLAY")
	tap_text:SetAllPoints(true)
	tap_text:SetFont(STANDARD_TEXT_FONT, 27, "OUTLINE")
	tap_text:SetShadowColor(0, 0, 0, 0.8)
	tap_text:SetShadowOffset(2, -2)
	tap_text:SetTextColor(1,1,0)
	tap_text:SetText("")	

	--Shadow Infusion
	pet_icon = CreateFrame("Frame", nil, UIParent)
	pet_icon:SetSize(size, size)
	pet_icon:SetPoint("CENTER", UIParent, "CENTER", -100, -82)
	pet_icon.cdtexture = CreateFrame("Cooldown", nil, pet_icon)
	pet_icon.cdtexture:SetReverse(true)
	pet_icon.cdtexture:SetAllPoints(true)
	pet_icon.cdtexture.noCooldownCount = false
	pet_icon.cdtexture:SetFrameLevel(1)
	pet_icon:Hide()

	pet_texture = pet_icon:CreateTexture(nil, "BACKGROUND")
	pet_texture:SetAllPoints(true)

	pet_overlay = CreateFrame("Frame", nil, pet_icon)
	pet_overlay:ClearAllPoints()
	pet_overlay:SetPoint("TOPLEFT", -3, 3)
	pet_overlay:SetPoint("BOTTOMRIGHT", 3, -3)

	pet_border = CreateFrame("Frame", nil, pet_icon)
	pet_border:SetAllPoints(true)
    pet_border:SetBackdrop(backdrop)

	pet_text = pet_icon.cdtexture:CreateFontString(nil, "OVERLAY")
	pet_text:SetAllPoints(true)
	pet_text:SetFont(STANDARD_TEXT_FONT, 27, "OUTLINE")
	pet_text:SetShadowColor(0, 0, 0, 0.8)
	pet_text:SetShadowOffset(2, -2)
	pet_text:SetTextColor(1,1,0)
	pet_text:SetText("")

	--Dark Simulacrum
	dsc_icon = CreateFrame("Frame", nil, UIParent)
	dsc_icon:SetSize(size, size)
	dsc_icon:SetPoint("CENTER", UIParent, "CENTER", 100, -82)
	dsc_icon.cdtexture = CreateFrame("Cooldown", nil, dsc_icon)
	dsc_icon.cdtexture:SetReverse(true)
	dsc_icon.cdtexture:SetAllPoints(true)
	dsc_icon.cdtexture:SetFrameLevel(1)
	dsc_icon:Hide()

	dsc_texture = dsc_icon:CreateTexture(nil, "BACKGROUND")
	dsc_texture:SetAllPoints(true)

	dsc_border = CreateFrame("Frame", nil, dsc_icon)
	dsc_border:SetAllPoints(true)
    dsc_border:SetBackdrop(backdrop)

	--Conversion
	con_icon = CreateFrame("Frame", nil, UIParent)
	con_icon:SetSize(size, size)
	con_icon:SetPoint("CENTER", UIParent, "CENTER", 49, -82)
	con_icon:Hide()

	con_texture = con_icon:CreateTexture(nil, "BACKGROUND")
	con_texture:SetAllPoints(true)

	con_border = CreateFrame("Frame", nil, con_icon)
	con_border:SetAllPoints(true)
    con_border:SetBackdrop(backdrop)
end

function infDK:ACTIVE_TALENT_GROUP_CHANGED()
	if pet_icon:IsShown() then
		self:Clear("Shadow Infusion")
	end
end

function infDK:UNIT_PET(_, unit)
	if unit ~= "player" then return end
	if not UnitExists("pet") then
		if pet_icon:IsShown() then
			self:Clear("Shadow Infusion")
		end
	end	
end

function infDK:UNIT_AURA(units)
	for unitId in pairs(units) do
		self:BucketUpdate(unitId)
	end
end

function infDK:BucketUpdate(unitId)
	if unitId == "player" then
		for buff in pairs(player_buffs) do
			local _, _, icon, count, _, duration, expirationTime = UnitBuff(unitId, buff)
			if icon then
				self:DKTracker(buff, icon, count, duration, expirationTime)
				if not player_buffs[buff] then
					player_buffs[buff] = true
				end
			else
				if player_buffs[buff] then
					player_buffs[buff] = false
					self:Clear(buff)
				end
			end
		end
	elseif unitId == "pet" then
		for buff in pairs(pet_buffs) do
			local _, _, icon, count, _, duration, expirationTime = UnitBuff(unitId, buff)
			if icon then
				self:DKTracker(buff, icon, count, duration, expirationTime)
				if not pet_buffs[buff] then
					pet_buffs[buff] = true
				end
			else
				if pet_buffs[buff] then
					pet_buffs[buff] = false
					self:Clear(buff)
				end
			end
		end
	end
end

function infDK:DKTracker(buff, icon, count, duration, expirationTime)
	if buff == "Blood Charge" then
		if expirationTime > 0 then
			local startTime = expirationTime - duration
			tap_icon.cdtexture:SetCooldown(startTime, duration)
			tap_icon.cdtexture:Show()
			tap_icon:Show()
		end
		tap_text:SetText(count)
		if count > 4 and count < 10 then
			tap_text:SetTextColor(1, .5, 0)
		elseif count >= 10 then
			tap_text:SetTextColor(1, 0, 0)
		else
			tap_text:SetTextColor(1, 1, 0)
		end
	elseif buff == "Dark Simulacrum" then
		if expirationTime > 0 then
			local startTime = expirationTime - duration
			dsc_icon.cdtexture:SetCooldown(startTime, duration)
			dsc_icon.cdtexture:Show()
			dsc_icon:Show()
		end
		local texture = GetSpellTexture(buff)
		if dsc_texture:GetTexture() ~= texture then
			dsc_texture:SetTexture(texture)
		end
	elseif buff == "Conversion" then
		if not con_icon:IsShown() then
			con_icon:Show()
		end
		if con_texture:GetTexture() ~= icon then
			con_texture:SetTexture(icon)
		end
	elseif buff == "Shadow Infusion" then
		if pet_icon.cdtexture.noCooldownCount == false then
			pet_icon.cdtexture.noCooldownCount = true
			pet_texture:SetTexture(icon)
		end
		if expirationTime > 0 then
			local startTime = expirationTime - duration
			pet_icon.cdtexture:SetCooldown(startTime, duration)
			pet_icon.cdtexture:Show()
			pet_icon:Show()
		end
		if count == 5 then
			ActionButton_ShowOverlayGlow(pet_overlay)
		end
		pet_text:SetText(count)
	elseif buff == "Dark Transformation" then
		if pet_icon.cdtexture.noCooldownCount == true then
			pet_icon.cdtexture.noCooldownCount = false
			pet_texture:SetTexture(icon)
		end
		if expirationTime > 0 then
			local startTime = expirationTime - duration
			pet_icon.cdtexture:SetCooldown(startTime, duration)
			pet_icon.cdtexture:Show()
			pet_icon:Show()
		end
		pet_text:SetText("")
	end
end

function infDK:Clear(buff)
	if buff == "Blood Charge" then
		tap_icon.cdtexture:Hide()
		tap_icon:Hide()
	elseif buff == "Dark Simulacrum" then
		dsc_icon.cdtexture:Hide()
		dsc_icon:Hide()
	elseif buff == "Conversion" then
		con_icon:Hide()
	elseif buff == "Shadow Infusion" then
		ActionButton_HideOverlayGlow(pet_overlay)
		--pet_overlay:StopAnimating()
		pet_icon.cdtexture:Hide()
		pet_icon:Hide()
	elseif buff == "Dark Transformation" then
		pet_icon.cdtexture:Hide()
		pet_icon:Hide()
	end
end

